/***************************************************************************
 *   Copyright (C) 2006 by Sergejs Melderis   *
 *   smelderis@yahoo.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "textencoder.h"
#include <qapplication.h>
#include "prefs.h"
#include "translitpairlist.h"
TextEncoder::TextEncoder()
{
    mTranslitPairList = TranslitPairListHelper::fromQStringList( Prefs::translitPairList() );
    qHeapSort(mTranslitPairList);
}

TextEncoder::~TextEncoder()
{
}

QString TextEncoder::russianToEnglish(const QString &ruText)
{
    return transliterate(ruText, true);
}

QString TextEncoder::englishToRussian(const QString &enText)
{
    return transliterate(enText, false);
}

QString TextEncoder::transliterate(const QString &inText, const bool ru2lat)
{
    QString text = inText;
    //QValueList<QPair<QString, QString> >::iterator iter;
    TranslitPairList::iterator iter;
    for (iter = mTranslitPairList.begin(); iter != mTranslitPairList.end(); iter++) {
      if (ru2lat)
        text.replace((*iter).first, (*iter).second);
      else
        text.replace((*iter).second, (*iter).first);
    }
    return text;
}

QString TextEncoder::encodeForLingvo(const QString &acText)
{
    // Lingvo uses url encoded html entity representation of words
    // i.e russian word "spasibo" (written in russian) will be represented as
    // %26%231089%3B%26%231087%3B%26%231072%3B%26%231089%3B%26%231080%3B%26%231073%3B%26%231086%3B
    // this is url encoding of
    // &#1089;&#1087;&#1072;&#1089;&#1080;&#1073;&#1086;
    // and this is just html entities (numbers between "&#" and ";" are just
    // unicode numbers of russian letters)
    // All of the unicode chars can be encoded this way
    // TODO: functions like uniTo1251 are now obsolet. Code cleanup needed.
    QString _acText = "";
    int l = acText.length();
    for (int i = 0; i < l; i++) {
        _acText += "%26%23" + QString::number(acText.at(i).unicode()) + "%3B";
    }
    return _acText;
}





